package com.map.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.map.entity.*;
import com.map.service.*;
import entity.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/bs/search")
public class SearchTagInfoController {

    @Autowired
    private LivingBuildingService livingBuildingService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private LivingBuildingRoomService livingBuildingRoomService;
    @Autowired
    private OfficeBuildingRoomService officeBuildingRoomService;
    @Autowired
    private PositionBuildService positionBuildService;

    @RequestMapping("/taginfo")
    public ResultBean searchTagInf(String keyWord){

        List<JSONObject> list = new ArrayList<>();
        System.out.println(keyWord);
        if(StringUtils.isEmpty(keyWord)){
            return  new ResultBean(400,"查询结果为空",list);
        }
        //办公楼房间
        QueryWrapper<OfficeRoomInfos> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("tag_info",keyWord).or().like("office_building_name",keyWord);
        List<OfficeRoomInfos> officeRoomInfos = officeBuildingRoomService.list(queryWrapper);
        //生活楼房间
        QueryWrapper<LivingRoomInfos> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like("tag_info",keyWord).or().like("living_building_name",keyWord);
        List<LivingRoomInfos> livingRoomInfosList = livingBuildingRoomService.list(queryWrapper1);

        if (!CollectionUtils.isEmpty(officeRoomInfos)){
            for (OfficeRoomInfos roomInfos : officeRoomInfos){
                JSONObject jsonObject = new JSONObject();
                OfficeBuild officeBuild = officeBuildingService.getById(roomInfos.getOfficeId());
                if (Objects.nonNull(officeBuild)){
                    jsonObject.put("id",officeBuild.getId());
                    jsonObject.put("name",officeBuild.getOfficeBuildingName());
                    jsonObject.put("zuobiao",officeBuild.getAnchor());
                    jsonObject.put("roomname",roomInfos.getRoomName());
                    jsonObject.put("tagInfo",roomInfos.getTagInfo());
                    list.add(jsonObject);
                }
            }
        }

        if (!CollectionUtils.isEmpty(livingRoomInfosList)){
            for (LivingRoomInfos roomInfos : livingRoomInfosList) {
                JSONObject jsonObject = new JSONObject();
                LivingBuilding livingBuilding = livingBuildingService.getById(roomInfos.getLivingId());
                if (Objects.nonNull(livingBuilding)){
                    jsonObject.put("id",livingBuilding.getId());
                    jsonObject.put("name",livingBuilding.getLivingBuilding());
                    jsonObject.put("zuobiao",livingBuilding.getAnchor());
                    jsonObject.put("roomname",roomInfos.getRoomName());
                    jsonObject.put("tagInfo",roomInfos.getTagInfo());
                    list.add(jsonObject);
                }
            }
        }

        if (list.size()!= 0){
            return new ResultBean(200,"楼栋查询成功",list);
        }else {
            return  new ResultBean(400,"查询结果为空",list);
        }
    }

    @RequestMapping("/list")
    public ResultBean searchBuildList() {
        QueryWrapper<BsPosition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<BsPosition> list = positionBuildService.list(queryWrapper);
        return new ResultBean(200,"查询成功",list);
    }

    @RequestMapping("/live/list")
    public ResultBean searchLiveList() {
        QueryWrapper<BsPosition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",2);
        List<BsPosition> list = positionBuildService.list(queryWrapper);
        return new ResultBean(200,"查询成功",list);
    }

    @RequestMapping("/info")
    public ResultBean searchBuildInfo(Integer buildId){
        OfficeBuild officeBuild = officeBuildingService.getById(buildId);
        QueryWrapper<OfficeRoomInfos> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("office_id",buildId);
        List<OfficeRoomInfos> officeRoomInfos = officeBuildingRoomService.list(queryWrapper);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("buildName",officeBuild.getOfficeBuildingName());
        jsonObject.put("buildImg",officeBuild.getImgUrl());
        jsonObject.put("detiled",officeBuild.getDetiledDescription());
        jsonObject.put("officeRooms",officeRoomInfos);
        jsonObject.put("collageName",officeBuild.getCollageName());
        jsonObject.put("zuobiao",officeBuild.getAnchor());
        return new ResultBean(200,"信息查询成功",jsonObject);
    }

    @RequestMapping("/live/info")
    public ResultBean searchLiveInfo(Integer buildId){
        LivingBuilding livingBuilding = livingBuildingService.getById(buildId);
        QueryWrapper<LivingRoomInfos> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("living_id",buildId);
        List<LivingRoomInfos> livingRoomInfosList = livingBuildingRoomService.list(queryWrapper);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("buildName",livingBuilding.getLivingBuilding());
        jsonObject.put("buildImg",livingBuilding.getImgUrl());
        jsonObject.put("detiled",livingBuilding.getDetiledDescription());
        jsonObject.put("officeRooms",livingRoomInfosList);
        jsonObject.put("collageName",livingBuilding.getCollageName());
        jsonObject.put("zuobiao",livingBuilding.getAnchor());
        return new ResultBean(200,"信息查询成功",jsonObject);
    }

}
